<template>
<view class="container">
	<view class="avatar">
	    <image :src="avatar"></image>
	</view>
	<view class="view_input1">
	    <view class="view_input_title2">机构名称:</view>
		<input class="view_input_input1" type="text" v-model="form.name"
		placeholder-class="placeholderClass" placeholder="请输入机构名称"/>
	</view>
	<view class="view_input1">
	    <view class="view_input_title2">联系人:</view>
		<input class="view_input_input1" type="text" v-model="form.contacts"
		placeholder-class="placeholderClass" placeholder="请输入联系人"/>
	</view>
	<view class="view_input1">
	    <view class="view_input_title2">电话/微信:</view>
		<input class="view_input_input1" type="text" v-model="form.phone"
		placeholder-class="placeholderClass" placeholder="请输入电话"/>
	</view>
	<view class="view_input1">
	    <view class="view_input_title2">招聘岗位:</view>
		<input class="view_input_input3" type="text" v-model="form.job"
		placeholder-class="placeholderClass" placeholder="招聘岗位" @click="jumpJob"
		disabled/>
		<image @click="jumpJob" style="width:40rpx;height:40rpx;" src="/static/img/right.png"></image>
	</view>
	<view class="view_input1">
	    <view class="view_input_title2">招聘人数:</view>
		<input class="view_input_input1" type="text" v-model="form.num"
		placeholder-class="placeholderClass" placeholder="招聘人数"/>
	</view>
	<view class="view_input1">
	    <view class="view_input_title2">薪资待遇:</view>
		<picker @change="bindMoney" :value="form.money" :range="money"
		class="view_input_input1">
			<view>{{money[form.money]}}</view>
		</picker>
	</view>

	<view class="view_input1">
	    <view class="view_input_title2">工作城市:</view>
		<input class="view_input_input3" type="text" v-model="form.city" @click="jumpCity"
		placeholder-class="placeholderClass" placeholder="工作城市" disabled/>
		<image @click="jumpCity" style="width:40rpx;height:40rpx;" src="/static/img/right.png"></image>
	</view>
	<view class="view_input1">
	    <view class="view_input_title2">具体地址:</view>
		<input class="view_input_input1" type="text" v-model="form.address"
		placeholder-class="placeholderClass" placeholder="具体地址"/>
	</view>
	<view class="view_else">
		<view class="view_input_title2">岗位描述:</view>
	</view>
	<view class="view_textarea">
		<textarea data-name="remark" auto-height maxlength="-1"
		placeholder="岗位描述" v-model="form.remark"/>
	</view>
	
	<view class="view_else">
		<view class="view_input_title2">营业执照:</view>
	</view>
	<view class="view_img">
		<image class="uploadImg" v-if="form.license" @click="upLoad('license')" :src="imgUrl + form.license"></image>
		<image class="uploadImg" v-else @click="upLoad('license')" :src="pubImg"></image>
	</view>
	<view class="view_else">
		<view class="view_input_title2">机构配图:</view>
	</view>
	<view class="view_img">
		<view class="imgContent" v-if="form.imgs.length > 0" v-for="(item,index) in form.imgs" :key="index">
			<image class="imgItem" :src="imgUrl + item"></image>
			<view class="delete" @click="imgDel(index)">删除</view>
		</view>
		<image class="uploadImg imgs" v-if="uploadShow" @click="upLoad('imgs')" :src="pubImg"></image>
	</view>
	<view style="height:120rpx;"></view>
	<button v-if="submittrue"  @click="companyAdd" class="buttonClass">提交</button>
</view>
</template>

<script>
import request from '@/util/request.js'
export default {
	data() {
		return {
			imgUrl:this.url,
			pubImg:'/static/img/pub.png',
			avatar:uni.getStorageSync('avatarUrl'),
			form:{
				imgs:[],
				city:''
			},
			moneyIndex:0,
			money:['面议','5k以下','5k~8k','8k~10k','10k以上'],
			submittrue:true,
			uploadShow:true
		}
	},
	onLoad:function(){
		this.getInfo()
	},
	methods: {
		getInfo:function(){
			var that = this
			var data = {uid:uni.getStorageSync('uid')}
			request({
				url: 'company/companyInfo',
				method: 'get',
				data:data,
				success: (res) => {
					that.form = res.data.data
				},
			})
		},
		bindMoney:function(e){
			this.form.money = e.detail.value
		},
		companyAdd:function(){
			var array = {
				'name':'机构名称必填',
				'contacts':'联系人必填',
				'phone':'联系电话必填',
				'job':'招聘岗位必填',
				'num':'招聘人数必填',
				'address':'地址必填',
				//'license':'营业执照必须上传',
				//'imgs':'店铺照片必须上传',
			  }
			for(var x in array){
			    if(!this.form[x]){
			        uni.showToast({
			        	title:array[x],
			        	icon: "none", 
			        	position:'bottom',
			        });
			        return;
			    }
			}
			this.submittrue = false
			//调接口 提交
			var that = this
			request({
				url: 'user/companyEdit',
				method: 'put',
				data:this.form,
				success: (res) => {
					uni.showToast({
						title:res.data.msg,
						icon: 'none',
						duration: 2000,
						success() {
							setTimeout(function() {
								uni.navigateBack();
							}, 2000);
						}
					})
				}
			})
		},
		upLoad:function(name){
			var token = 'tymy';
			var that = this
			uni.chooseImage({
				count: 1,
				sizeType:['copressed'],
				success(res){
					uni.showLoading({
					    title: '上传中'
					});
					//因为有一张图片， 输出下标[0]， 直接输出地址
					var imgFiles = res.tempFilePaths[0]
					var uper = uni.uploadFile({
						// 需要上传的地址
						url:that.imgUrl+'/api/app/upload',
						// filePath  需要上传的文件
						filePath: imgFiles,
						name:'img',
						header: {
						    // "Content-Type": "multipart/form-data",
						    // 'accept': 'application/json',  //若有token，此处换上你的token，没有的话省略
							'Authorization' : 'Bearer' + token
						},
						formData:{
							//name:'img'
						},
						success(res1) {
							uni.hideLoading();
							// 显示上传信息
							if(name == 'license'){
								that.form[name] = res1.data
							}else{
								if(!that.form.imgs){
									that.form.imgs = []
								}
								if(that.form.imgs.length >= 5){
									that.uploadShow = false
								}
								that.form.imgs.push(res1.data)
							}
						}
					});
				}
			})
		},
		imgDel:function(index){
			this.form.imgs.splice(index,1)
			this.uploadShow = true
		},
		jumpJob:function(){
			var url = '/pages/login/post/index?post='+this.form.job
			uni.navigateTo({
				url:url
			});
		},
		jumpCity:function(){
			var url = '/pages/city/index'
			uni.navigateTo({
				url:url
			});
		}
	}
}
</script>

<style>
.view_input_input3{
	width:50%;
	height:70rpx;
	line-height: 70rpx;
	padding-left:15rpx;
}
.avatar{background: #ff861a;width:100%;height:100px;display: flex;justify-content:center;}
.avatar image{width:80px;height:80px;border-radius: 50%;margin-top:10px;}
.view_else{
	width:100%;
	display: flex;
	flex-direction: row;
	height:70rpx;
	line-height: 70rpx;
}
.view_textarea{
	width:100%;
	padding:15rpx 15rpx 15rpx 15rpx;
}
.view_textarea textarea {
	width:93%;
	border:1px dashed #CCCCCC;
	min-height:200rpx;
	padding:10rpx 10rpx 10rpx 10rpx;
}
.view_img{
	padding:20rpx 20rpx 20rpx 30rpx;
	display: flex;
	flex-direction: row;
	flex-wrap: wrap;
}
.uploadImg{
	width:200rpx;
	height:180rpx;
	border:1px dashed #CCCCCC;
	margin-right:15rpx;
}
.imgContent{
	width:200rpx;
	height:180rpx;
	margin-right:15rpx;
}
.imgItem{
	width:100%;
	height:100%;
	border:1px dashed #CCCCCC;
}
.delete{
	position: relative;
    top: -25px;
	width:200rpx;
	background: #e60000;
	color:#fff;text-align: center;
}
</style>
